package crosstime.android.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import crosstime.android.cexplorer.R;

public class MainToolBar extends BaseToolBarWidget {

	private Animation myAnimation = null;
	public boolean IsMenuShown = true;
	private INotifier mNotifier = null;
	
	public MainToolBar(Context context){
		super(context);
		setMainView();
	}
	
	public MainToolBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		setMainView();
	}
    
    private void setMainView(){
    	if(mContentLayout == null)
    		return;
    	mContentLayout.removeAllViews();
    	LinearLayout toolView = (LinearLayout) mInflater.inflate(R.layout.tool_bar_main, null);
		RelativeLayout btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_multi);
		btn.setOnClickListener(mMainClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_home);
		btn.setOnClickListener(mMainClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_up);
		btn.setOnClickListener(mMainClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_view);
		btn.setOnClickListener(mMainClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_sort);
		btn.setOnClickListener(mMainClickListener);
		mContentLayout.addView(toolView);
		mContentLayout.invalidate();
    }
    
    private OnClickListener mMainClickListener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			
			switch(v.getId())
			{
			case R.id.tool_menu_home:
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_MAIN_HOME);
				
				break;
			case R.id.tool_menu_sort:
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_MAIN_SORT);
				
				break;
			case R.id.tool_menu_up:
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_MAIN_UP_LEVEL);
				break;
			case R.id.tool_menu_view:
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_MAIN_VIEW);
				break;
			case R.id.tool_menu_multi:
				showMenu(false);
				setMultiView();
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_SELECTION_ON);
				showMenu(true);
				break;
				
			default:
				break;
			}
		}
    };
    
    
    private void setMultiView(){
    	if(mContentLayout == null)
    		return;
    	mContentLayout.removeAllViews();
		LinearLayout toolView = (LinearLayout) mInflater.inflate(R.layout.tool_bar_multi, null);
		RelativeLayout btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_multi_plus);
		btn.setOnClickListener(mMultiClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_multi_minus);
		btn.setOnClickListener(mMultiClickListener);
		btn = (RelativeLayout) toolView.findViewById(R.id.tool_menu_multi_back);
		btn.setOnClickListener(mMultiClickListener);
		mContentLayout.addView(toolView);
		mContentLayout.invalidate();
    }

    
    private OnClickListener mMultiClickListener = new OnClickListener(){

		@Override
		public void onClick(View v) {
			
			switch(v.getId())
			{
			case R.id.tool_menu_multi_plus:
				if(v.getTag() != null && v.getTag().equals("hold"))
				{
					setBtnHold(v, false);
					if(mNotifier != null)
						mNotifier.notify(INotifier.MODE_SELECTION_ON);
				}else
				{
					RelativeLayout btn = (RelativeLayout) findViewById(R.id.tool_menu_multi_minus);
					setBtnHold(btn, false);
					setBtnHold(v, true);
					if(mNotifier != null)
						mNotifier.notify(INotifier.MODE_SELECTION_PLUS);
				}
				break;
			case R.id.tool_menu_multi_minus:
				if(v.getTag() != null && v.getTag().equals("hold"))
				{
					setBtnHold(v, false);
					if(mNotifier != null)
						mNotifier.notify(INotifier.MODE_SELECTION_ON);
				}else
				{
					RelativeLayout btn = (RelativeLayout) findViewById(R.id.tool_menu_multi_plus);
					setBtnHold(btn, false);
					setBtnHold(v, true);
					if(mNotifier != null)
						mNotifier.notify(INotifier.MODE_SELECTION_MINUS);
				}
				break;
			case R.id.tool_menu_multi_back:
				showMenu(false);
				setMainView();
				if(mNotifier != null)
					mNotifier.notify(INotifier.MODE_SELECTION_BACK);
				showMenu(true);
				break;
			default:
				break;
			}
			
		}
    };
    
    private void setBtnHold(View v, boolean bHold){
    	if(bHold)
    	{
			v.setBackgroundResource(R.drawable.btn_bg_pressed);
			v.setTag("hold");
    	}else
    	{
			v.setBackgroundResource(R.drawable.btn_bg_normal);
			v.setTag("none");
    	}
    }
    
	public void showMenu(boolean bShow)
	{
		if(myAnimation != null && myAnimation.hasStarted())
			myAnimation.cancel();
		this.clearAnimation();
		this.setVisibility(bShow?View.VISIBLE:View.GONE);
		IsMenuShown = bShow;

		setTranslateAnimation(bShow);
		this.startAnimation(myAnimation);
	}
	
	private void setTranslateAnimation(boolean bMoveOut)
	{
		float fromX = 0.0f, toX = 0.0f, 
			  fromY = 0.0f, toY = 0.0f;

		fromX = 0.0f;
		toX = 0.0f;
		if(bMoveOut)
		{
			fromY = -100.0f;
			toY = 0.0f;
		}
		else
		{
			fromY = 0.0f;
			toY = -100.0f;
		}

		myAnimation=new TranslateAnimation(fromX, toX, fromY, toY); 
		myAnimation.setInterpolator(new AccelerateDecelerateInterpolator());   
		myAnimation.setDuration(150);  
	}
	
	public void setNotify(INotifier notify){
		mNotifier = notify;
	}
	
	public interface INotifier {
		public static final int MODE_MAIN_HOME = 0x1001;
		public static final int MODE_MAIN_UP_LEVEL = 0x1002;
		public static final int MODE_MAIN_VIEW = 0x1003;
		public static final int MODE_MAIN_SORT = 0x1004;
		
		public static final int MODE_SELECTION_BACK = 0x3000;
		public static final int MODE_SELECTION_ON = 0x3001;
		public static final int MODE_SELECTION_PLUS = 0x3002;
		public static final int MODE_SELECTION_MINUS = 0x3003;
		
		public void notify(int infoId);
	}
}
